﻿#include "Singleton.h"
#include <iostream>

// 示例类
class Example {
public:
    Example() {
        std::cout << "Example constructed" << std::endl;
    }

    ~Example() {
        std::cout << "Example destructed" << std::endl;
    }

    void doSomething() {
        std::cout << "Example instance: " << this << std::endl;
    }
};

class Foo {
public:
    void doSomething() {
        std::cout << "Foo instance: " << this << " is doing something." << std::endl;
    }

private:
    friend class Singleton<Foo>; // 允许 Singleton<Foo> 访问私有构造函数
    Foo() {
        std::cout << "Foo constructed" << std::endl;
    }
    ~Foo() {
        std::cout << "Foo destructed" << std::endl;
    }
};

class Bar {
public:
    void doSomethingElse() {
        std::cout << "Bar instance: " << this << " is doing something else." << std::endl;
    }

private:
    friend class Singleton<Bar>; // 允许 Singleton<Bar> 访问私有构造函数
    Bar() {
        std::cout << "Bar constructed" << std::endl;
    }
    ~Bar() {
        std::cout << "Bar destructed" << std::endl;
    }
};

int main(int argc, char *argv[])
{
    //QCoreApplication a(argc, argv);

    //test SingletonStatic
    {
        // 使用模板 Singleton 获取 Example 实例
        Example& instance1 = SingletonStatic<Example>::instance();
        instance1.doSomething();

        Example& instance2 = SingletonStatic<Example>::instance();
        instance2.doSomething();

        // 检查是否是同一实例
        std::cout << "Are instances equal? " << std::boolalpha
                  << (&instance1 == &instance2) << std::endl;
    }





    // test Singleton
    {
        // 获取 Singleton 实例并调用方法
        Foo* fooInstance = Singleton<Foo>::instance();
        fooInstance->doSomething();

        Foo* fooInstance2 = Singleton<Foo>::instance();
        fooInstance2->doSomething();

        Bar* barInstance = Singleton<Bar>::instance();
        barInstance->doSomethingElse();
    }


    //return a.exec();
}
